<?php
namespace app\api\controller;

use app\api\controller\OssController as OSSApi;
use app\api\controller\KbxToken;

class GoogleMap{

    //接口地址
    protected $_base_uri = null;
    protected $_token = null;
    protected $_kbxToken = null;
    //初始化
    public function __construct($base_uri="")
    {
        if(!empty($base_uri)){
            $this->_base_uri = $base_uri;
        }else{
            $this->_base_uri = "https://d.kwibon.cn";
        }

        //身份验证
        $kbxToken = new KbxToken();
        $this->_kbxToken=$kbxToken;
        $this->_token='Bearer '.$kbxToken->kbxToken();
    }

    //获取poi的谷歌图片资源 单点的标记
    public function getGoogleMapImgByPoint($lat="",$lng="",$room=13)
    {
        if(empty($lat) || empty($lng)){
            return "";
        }
        $api_uri = "/travelDesign/getGoogleMapPoint";
        $url = $this->_kbxToken->url.$api_uri;
        $data['width'] = 600;
        $data['height'] = 300;
        $data['zoom'] = $room;
        $data['location'] = [
            'lat'=>$lat,
            'lng'=>$lng,
        ];
        $list = json_encode($data);
        $res=$this->_kbxToken->post_data($url,$list,$this->_token);
        $res=$this->_kbxToken->object_array($res);

        $result = json_decode($res,true);
        if(!is_array($result)){
            return json(['code'=>-1,"msg"=>$result]);
        }else{
            if(isset($result['img']) && !empty($result['img'])){
                return $result['img'];
            }else{
                return "";
            }
        }
    }

    //获取poi的谷歌图片资源 线路的标记
    //$latLngArr = [
    //    ['lat'=>51.0449777,'lng'=>-114.06113349999998,'title'=>'尼亚加拉瀑布001'],
    //    ['lat'=>51.04430800000001,'lng'=>-114.06309140000002,'title'=>'尼亚加拉瀑布002'],
    //    ['lat'=>52.04430800000001,'lng'=>-115.06309140000002,'title'=>'尼亚加拉瀑布003'],
    //];
    public function getGoogleMapImgByRoad($latLngArr=[],$width = 1200,$height = 600)
    {
        if(empty($latLngArr) || !is_array($latLngArr)){
            return "";
        }
        $api_uri = "/travelDesign/getGoogleMapDirection";
        $url = $this->_kbxToken->url.$api_uri;
        $data['width'] = $width;
        $data['height'] = $height;
        $data['room'] = 10;
        $data['latLngArr'] = $latLngArr;
        $list = json_encode($data);
        $res=$this->_kbxToken->post_data($url,$list,$this->_token);
        $res=$this->_kbxToken->object_array($res);
        $result = json_decode($res,true);
        if(!is_array($result)){
            return json_encode(['code'=>-1,"msg"=>$result]);
        }else{
            if(isset($result['img']) && !empty($result['img'])){
                $img = (new OSSApi)->saveRemoteImgToOSS($result['img']);
                return $img;
            }else{
                return "";
            }
        }
    }

    //定制系统 通过经纬度获取线路标记
    public function getGoogleMapRoadImageByLatLngs($latLngs=[],$width=640,$height=400,$scale=2,$zoom=8)
    {
        if(empty($latLngs) || !is_array($latLngs)) return "";
        if(!in_array($scale,[1,2,4])) return "";

        $baseurl = "http://push.kwibon.cn/maps/api/staticmap?";
        $size = $width.'x'.$height;//图片大小
        $maptype = 'roadmap';//线路类型
        $key = "AIzaSyBICWcSC4Iosl2raNVBEw7Qt3FmD28d8xE";//地图的key 从谷歌地图开发者平台获取
        $mapurl = $baseurl.http_build_query(compact("size","scale","maptype","key"));
        $path = "path=color:0x0000ff|weight:5|";
        $markers = [];
        $list = [];
        $count = count($latLngs);
        foreach ($latLngs as $k=>$v){
            $latlng = $v['lat'].','.$v['lng'];
            $marker = 'markers=color:';
            if($k===($count-1)){
                $marker .= 'green';
            }elseif($k===0){
                $marker .= 'red';
            }else{
                $marker .= 'blue';
            }
            $markers[] = $marker.'%7Clabel:'.($k+1).'%7C'.$latlng;
            $list[] = $latlng;
        }
        if(empty($list)) return "";
        if(empty($markers)) return "";
        $path .= implode('|',$list);
        $markers = implode('&',$markers);
        $mapurl = $mapurl.'&'.$path.'&'.$markers;
        //如果地图上只有一个标记 则地图比例设置为8
        if(count($latLngs) == 1) $mapurl .= "&zoom=".$zoom;
        return (new OSSApi)->saveRemoteImgToOSS($mapurl);
    }

    //谷歌地址位置检索
    public function getAddressInfo($input='',$language='',
                                   $fields='name,formatted_address,geometry,place_id,types',
                                   $key='AIzaSyDGFVgrdJNbNnCkOT5li6u1uLibd08EBdU')
    {
        if(empty($input)) return '请输入关键字';
        $inputtype = 'textquery';
        $url = 'https://push.kwibon.cn/mapProxy/findplacefromtext?';
        $url .= http_build_query(compact('input','inputtype','fields','key','language'));
        $result = (new Common())->http($url);

        return $result;
    }

}